import { Component, OnInit, Input, Output, EventEmitter, ViewChild } from '@angular/core';

import { CudTableService } from '../../../../public/serve/cud-table/cud-table.service';
import { HintsService } from '../../../../public/serve/hints/hints.service';
import { RemoveTableService } from '../../../../public/serve/remove-table/remove-table.service';
import { PaginationComponent } from '../../../../public/pagination/pagination.component';

import { appProductUrl } from '../../../product.url';
import { product } from '../../../product.data';
import {appSynonymUrl} from '../../../../synonym/synonym.url';
import {Service} from '../../../../server/server';

declare var $: any;

@Component({
    selector: 'app-product-indexing-task-list-assign-to',
    templateUrl: './product-indexing-task-list-assign-to.component.html'
})
export class ProductIndexingTaskListAssignToComponent implements OnInit {
    total: number = 0;
    naturalNum: number = 0;
    list: Array<object>;

    isChose: boolean = false;
    queryCriteria: string = '';

    @Input() selectedAssignPersonsNames: string;    //选中的分配人(字符串)
    selectedAssignUserNames: Array<string> = [];         //选中的分配人(数组)
    @Input() taskName: string;                      //任务名称
    @Output() assignPersonsNames = new EventEmitter<string>();
    @ViewChild('pagination') pagination: PaginationComponent;

    constructor(
        private request: CudTableService,
        private hintsService: HintsService,
        private server:Service,
        public removeTableService: RemoveTableService) { }


    ngOnInit() {
    }
    getSelectedAssignPeople() {
        //选中原先的
        if (this.selectedAssignPersonsNames) {
            this.selectedAssignUserNames = this.selectedAssignPersonsNames.split(',');
        }
    }
    getTaskAssignList() {
        this._cancelChecked();
        let params = this.getTaskAssignListParams();
        this.request.postRequest(this.server.baseUrl +appProductUrl.getPusersList, params).then((res: any) => {
            if (res.result_code === '0000') {
                this.list = res.result;
                this.total = res.length;
                this.naturalNum = product.pagination.naturalNum;
                if(this.selectedAssignPersonsNames && this.selectedAssignPersonsNames.length > 0) {
                    for (let item of this.list) {
                        for (let child of this.selectedAssignUserNames) {
                            if (item['userCode'] == child) {
                                item['isChecked'] = true;
                                this.removeTableService.removeChose(true, item, this.list.length)['bool'];
                            }
                        }
                    }
                }
            } else {
                this.hintsService.messages(res.message, res.result_code);
            }
            return;
        })
            .catch((err: any) => {
                this.hintsService.messages('error', 'error');
                return;
            });
    }

    getTaskAssignListParams() {
        return {
            token: product.retrieval.token,
            pageNumber: product.pagination.pageNumber,
            pageSize: product.pagination.pageSize,
            id: product.retrieval.id,
            queryCriteria: this.queryCriteria,
            queryType: 1,
            startCreateTime: '',
            endCreateTime: '',
            endLoginTime: ''
        }
    }


    /**
     * 单选
     * @param bool
     * @param child
     * @param length
     */
    _chose(bool, child, length) {
        if (!bool) {
            for (let item of this.list) {
                if (item['userCode'] === child.userCode) {
                    item['isChecked'] = !bool;
                }
            }
            var index = this.selectedAssignUserNames.indexOf(child.userCode); 
            if (index > -1) { 
                this.selectedAssignUserNames.splice(index, 1); 
            } 
        }else {
            this.selectedAssignUserNames.push(child.userCode);
        }

        this.removeTableService.removeChose(bool, child, length)['bool'];
    }

    /**
     * 重置表格复选框、选中的ID
     */
    public _cancelChecked() {
        this.isChose = false;
        this.list = [];
    }

    public dismissModal() {
        this.assignPersonsNames.emit(this.selectedAssignUserNames.toString());
        $('#assignPersons').modal('hide');
    }

    clearQueryCriteria() {
        // this.selectedAssignPersonsNames = '';
        this.selectedAssignUserNames = [];
        this.queryCriteria = '';
        this.pagination.currentPage = 1;
        this.pagination.pageSize = 20;
        product.pagination.pageNumber = 1;
        product.pagination.pageSize = 20;
        product.pagination.naturalNum = 0;
        this.removeTableService.removeAll(false, '', '');
    }

    /**
      * 分页
      * @param pagination 
      */
    _pagination(pagination: Array<any>) {
        product.pagination.pageNumber = pagination[0].pageNumber;
        product.pagination.pageSize = pagination[0].pageSize;
        product.pagination.naturalNum = (product.pagination.pageNumber - 1) * product.pagination.pageSize;
        this.getTaskAssignList();
    }

    /**
     * 搜索
     * @param val
     */
    _retrieval(val: string) {
        this.queryCriteria = val.trim();
        this.getTaskAssignList();
    }

}

